<template>
  <div class="orders">
    <!-- 面包屑区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <!-- 搜索区域 -->
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入内容" clearable>
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 数据表单区域 -->
      <el-table :data="ordersList" border>
        <!-- 表单索引 -->
        <el-table-column type="index"></el-table-column>
        <!-- 订单编号 -->
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <!-- 订单价格 -->
        <el-table-column prop="order_price" label="订单价格" width="140"></el-table-column>
        <!-- 是否付款 -->
        <el-table-column prop="pay_status" label="是否付款" width="140">
          <template v-slot="scope">
            <el-tag type="success" v-if="!scope.row.pay_status">已付款</el-tag>
            <el-tag type="warning" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <!-- 是否发货 -->
        <el-table-column prop="is_send" label="是否发货" width="140"></el-table-column>
        <!-- 下单时间 -->
        <el-table-column prop="create_time" label="下单时间" width="220">
          <template v-slot="scope">
            {{ scope.row.create_time | dataformat }}
          </template>
        </el-table-column>
        <!-- 操作栏 -->
        <el-table-column label="操作" width="180">
          <el-tooltip effect="dark" content="修改订单信息" placement="bottom">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog"></el-button>
          </el-tooltip>
          <el-tooltip effect="dark" content="物流信息" placement="bottom">
            <el-button type="success" icon="el-icon-location" size="mini" @click="showProgress"></el-button>
          </el-tooltip>
        </el-table-column>
      </el-table>

      <!-- 分页选择区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>

      <!-- 修改订单信息对话框 -->
      <el-dialog
        title="修改订单信息"
        :visible.sync="editDialogVisible"
        width="40%"
        @close="editDialogClose">
        <!-- 表单区域 -->
        <el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="100px">
          <el-form-item label="省市区/县" prop="address">
            <!-- 级联选择器 -->
            <el-cascader
              v-model="editForm.address"
              clearable
              :options="cityData"
              :props="{ expandTrigger: 'hover' }">
            </el-cascader>
          </el-form-item>
          <el-form-item label="详细地址" prop="detailAddress">
            <el-input v-model="editForm.detailAddress"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editDialogVisible = false">确 定</el-button>
        </span>
      </el-dialog>

      <!-- 物流进度对话框 -->
      <el-dialog
        title="物流进度"
        :visible.sync="progressDialogVisible"
        width="40%">
        <!-- 时间线区域 -->
        <el-timeline>
          <el-timeline-item
            v-for="(activity, index) in progressList"
            :key="index"
            :timestamp="activity.time">
            {{activity.context}}
          </el-timeline-item>
        </el-timeline>
        <span slot="footer" class="dialog-footer">
          <el-button @click="progressDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="progressDialogVisible = false">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { getOrderList, getProgressMessage } from 'network/order'
import cityData from './citydata'

export default {
  name: "Orders",
  data() {
    return {
      // 从js文件中导入城市信息
      cityData,

      // 查询对象
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },

      // 订单数据列表
      ordersList: [
        
      ],

      // 订单总量
      total: 0,

      // 物流数据数组  接口请求已失效
      progressList: [
        {
          time: "2018-05-10 09:39:00",
          ftime: "2018-05-10 09:39:00",
          context: "已签收,感谢使用顺丰,期待再次为您服务",
          location: ""
        },
        {
          time: "2018-05-10 08:23:00",
          ftime: "2018-05-10 08:23:00",
          context: "[北京市]北京海淀育新小区营业点派件员 顺丰速运 95338正在为您派件",
          location: ""
        },
        {
          time: "2018-05-10 07:32:00",
          ftime: "2018-05-10 07:32:00",
          context: "快件到达 [北京海淀育新小区营业点]",
          location: ""
        },
        {
          time: "2018-05-10 02:03:00",
          ftime: "2018-05-10 02:03:00",
          context: "快件在[北京顺义集散中心]已装车,准备发往 [北京海淀育新小区营业点]",
          location: ""
        },
        {
          time: "2018-05-09 23:05:00",
          ftime: "2018-05-09 23:05:00",
          context: "快件到达 [北京顺义集散中心]",
          location: ""
        },
        {
          time: "2018-05-09 21:21:00",
          ftime: "2018-05-09 21:21:00",
          context: "快件在[北京宝胜营业点]已装车,准备发往 [北京顺义集散中心]",
          location: ""
        },
        {
          time: "2018-05-09 13:07:00",
          ftime: "2018-05-09 13:07:00",
          context: "顺丰速运 已收取快件",
          location: ""
        },
        {
          time: "2018-05-09 12:25:03",
          ftime: "2018-05-09 12:25:03",
          context: "卖家发货",
          location: ""
        },
        {
          time: "2018-05-09 12:22:24",
          ftime: "2018-05-09 12:22:24",
          context: "您的订单将由HLA（北京海淀区清河中街店）门店安排发货。",
          location: ""
        },
        {
          time: "2018-05-08 21:36:04",
          ftime: "2018-05-08 21:36:04",
          context: "商品已经下单",
          location: ""
        }
      ],

      // 修改用户信息对话框的显示和隐藏
      editDialogVisible: false,

      // 查看物流对话框的显示和隐藏
      progressDialogVisible: false,

      // 修改信息 对象
      editForm: {
        address: [],
        detailAddress: ''
      },


      // 修改信息表单用户规则
      editFormRules: {
        address: [
          { required: true, message: '请选择省市区/县', trigger: 'blur' }
        ],
        detailAddress: [
          { required: true, message: '请填写详细地址', trigger: 'blur' }
        ]
      }
    }
  },

  created() {
    this.getOrderList()
  },

  methods: {
    // 获取订单数据列表
    getOrderList() {
      getOrderList(this.queryInfo).then(res => {
        if(res.meta.status !== 200) this.$message.error(res.meta.msg)
        this.ordersList = res.data.goods
        this.total = res.data.total
        // console.log(res);
      }).catch(err => this.$message.error(err))
    },

    // 分页的每页条数发生改变触发事件
    handleSizeChange(pageSize) {
      this.queryInfo.pagesize = pageSize
      this.getOrderList()
    },

    // 当前页发生改变触发事件
    handleCurrentChange(currentPage) {
      this.queryInfo.pagenum = currentPage
      this.getOrderList()
    },

    // 展示 修改订单信息 对话框
    showEditDialog() {
      this.editDialogVisible = true
    },

    // 展示 物流进度 对话框
    showProgress() {
      this.progressDialogVisible = true
      // 获取物流信息网络请求
      getProgressMessage().then(res => {
        if(res.meta.status !== 200) return this.$message.error('error')
          this.progressList = res.data
      }).catch(err => this.$message.error('err'))
    },

    // 监听修改信息 对话框的关闭事件
    editDialogClose() {
      this.$refs.editFormRef.resetFields()
    }
  }
}
</script>

<style>
  .el-breadcrumb,
  .el-row {
    margin-bottom: 20px;
  }

  .el-cascader {
    width: 100%;
  }
</style>